package com.general.framework.xiaoAoJavaMianShi.collections;

import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

public class RandomStringGenerator<T> implements Iterable<T>  {
    private final List<T> list;

    public RandomStringGenerator(List<T> list) {
        this.list = list;
    }

    @Override
    public Iterator<T> iterator() {
        return new Iterator<T>() {
            @Override
            public boolean hasNext() {
                return true;
            }

            @Override
            public T next() {
                return list.get((int) (Math.random() * list.size()));
            }
        };
    }

    public static void main(String[] args) {
        List<String> list = Arrays.asList("a", "b", "c", "d", "e");
        RandomStringGenerator<String> generator = new RandomStringGenerator<>(list);
        Iterator<String> iterator = generator.iterator();

        for (int i = 0; i < 10; i++) {
            System.out.println(iterator.next());
        }
        // for (String s : generator) {
        //     System.out.println(s);
        // }
    }
}
